﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class HYPipeConnectRadiatorBatch : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			if (commandData == null)
			{
				throw new System.ArgumentNullException("commandData", "is null");
			}
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			Selection selection = activeUIDocument.Selection;
			if (!YJKRevitTools.ViewCheck(activeUIDocument, new List<ViewType>
			{
				(ViewType)1,
				(ViewType)2,
				(ViewType)4
			}))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			Result result;
			try
			{
				ISelectionFilter selectionFilter = new PipeAndEquipmentSelectionFilter();
				ISelectionFilter selectionFilter2 = new PipeSelectionFilter();
				ISelectionFilter selectionFilter3 = new EquipmentSelectionFilter();
				HYPipeConnect hypipeConnect = new HYPipeConnect();
				string text = "水管连接散热器";
				int num = 0;
				transaction = new Transaction(document, text);
				transaction.Start();
				for (;;)
				{
					SubTransaction subTransaction = new SubTransaction(document);
					try
					{
						if (num != 0)
						{
							RevitVersionFuncs.ClearSelection(selection);
						}
						num++;
						IList<Pipe> list = new List<Pipe>();
						IList<FamilyInstance> list2 = new List<FamilyInstance>();
						FamilyInstance familyInstance = null;
						new List<Element>();
						IList<Element> list3 = new List<Element>();
						IList<Element> list4 = new List<Element>();
						if (selection.GetElementIds().Any<ElementId>())
						{
							foreach (ElementId elementId in selection.GetElementIds())
							{
								list3.Add(document.GetElementById(elementId));
							}
						}
						bool flag = true;
						bool flag2 = true;
						foreach (Element element in list3)
						{
							if (element is Pipe)
							{
								list4.Add(element);
								flag = false;
							}
							else if (-2001140 == element.Category.Id.IntegerValue)
							{
								list4.Add(element);
								flag2 = false;
							}
						}
						RevitVersionFuncs.ClearSelection(selection);
						if (flag && flag2)
						{
							IEnumerator<Element> enumerator2 = selection.PickElementsByRectangle(selectionFilter, "请框选水管和散热器").GetEnumerator();
							{
								while (enumerator2.MoveNext())
								{
									Element item = enumerator2.Current;
									list4.Add(item);
								}
								goto IL_253;
							}
						}
						if (flag)
						{
							IEnumerator<Element> enumerator2 = selection.PickElementsByRectangle(selectionFilter2, "请框选水管").GetEnumerator();
							{
								while (enumerator2.MoveNext())
								{
									Element item2 = enumerator2.Current;
									list4.Add(item2);
								}
								goto IL_253;
							}
						}
						if (flag2)
						{
							foreach (Element item3 in selection.PickElementsByRectangle(selectionFilter3, "请框选散热器"))
							{
								list4.Add(item3);
							}
						}
						IL_253:
						int num2 = 0;
						foreach (Element element2 in list4)
						{
							if (element2 is Pipe)
							{
								list.Add(element2 as Pipe);
							}
							else if (element2 is FamilyInstance)
							{
								list2.Add(element2 as FamilyInstance);
								familyInstance = (element2 as FamilyInstance);
								num2++;
							}
						}
						subTransaction.Start();
						if (familyInstance != null || list.Count >= 1)
						{
							if (familyInstance == null || list.Count < 1)
							{
								MessageBox.Show("至少选择一个散热器和水管", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
							}
							else if (num2 == 1)
							{
								if (!hypipeConnect.NewBeatchPipeEquipment(familyInstance, ref list, 0))
								{
									MessageBox.Show("连接失败", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
								}
							}
							else if (num2 > 1 && !hypipeConnect.NewBeatchPipeEquipment(list2, list))
							{
								MessageBox.Show("连接失败", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
							}
						}
						RevitVersionFuncs.ClearSelection(selection);
						document.Regenerate();
						subTransaction.Commit();
					}
					catch (Autodesk.Revit.Exceptions.OperationCanceledException)
					{
						result = Autodesk.Revit.UI.Result.Succeeded;
						break;
					}
					catch (Exception ex)
					{
						MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
					}
					finally
					{
						if (subTransaction != null)
						{
							TransactionStatus status = subTransaction.GetStatus();
							if ((int)status == 1)
							{
								subTransaction.Commit();
							}
							else if ((int)status == 5)
							{
								subTransaction.RollBack();
							}
						}
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex2)
			{
				MessageBox.Show(ex2.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				TransactionStatus status2 = transaction.GetStatus();
				if ((int)status2 == 1)
				{
					transaction.Commit();
				}
				else if ((int)status2 == 5)
				{
					transaction.RollBack();
				}
			}
			return result;
		}
	}
}
